/**
 * Created by Administrator on 2016/9/6.
 */

var router = require("express").Router();
var withdrawDao = require('./withdrawModel');
var accountDao = require('../account/accountModel');

var moment = require('moment');

function getWithdrawList(req,res){
    if(req.body != null){
        var page = req.body.page;
        var size = req.body.size;
    }else{
        return res.json(200,'error');
    }
    var object = {
        results:[],
        counts:0
    }
    withdrawDao.getWithdrawList(page,size,function(err,results){
        if(err){
            return res.json(500,err);
        }
        object.results = results;
        withdrawDao.getWithdrawCount(function(err,counts){
            if(err){
                return res.json(500,err);
            }
            object.counts = counts;
            return res.json(200,object);
        })

    })

}

/*
*
* 更新状态，将account表中的balance_to_cash清零，最后增加流水信息
* */
function changeWithdrawStatus(req,res){
    console.log(req.query);
    if(req.query){
        var withdrawId = req.query.withdrawId;
        var withdrawOrderNo = req.query.withdrawOrderNo;
        var userId = req.query.userId;
        var money = req.query.money;
    }else{
        return res.json(200,true);
    }
    withdrawDao.updateStatus(withdrawId,function(err,data){   //状态更新
        if(err){
            return res.json(500,err);
        }
        accountDao.updateBalanceToCash(userId,function(err,data){
            if(err){
                return err;
            }
            accountDao.addAccountBill(withdrawOrderNo,money,userId,function(err,data){
                if(err){
                    return err;
                }
                return res.json(200,true);
            })
        })
    })

}























router.post("/getWithdrawList", getWithdrawList);
router.get("/changeWithdrawStatus", changeWithdrawStatus);

module.exports = router;